package com.ys.service.base.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.config.MenuId;
import com.ys.dto.AddDto;
import com.ys.entity.base.TbUserPayApply;
import com.ys.entity.base.TbUserPayApplyItem;
import com.ys.entity.base.TbUserPaySlip;
import com.ys.entity.flow.YspModel;
import com.ys.entity.sys.TsUserChange;
import com.ys.entity.sys.YsCol;
import com.ys.entity.sys.YsMenu;
import com.ys.enume.flow.FlowEnum;
import com.ys.mapper.base.TbUserPayApplyItemMapper;
import com.ys.mapper.base.TbUserPayApplyMapper;
import com.ys.mapper.base.TbUserPaySlipMapper;
import com.ys.mapper.business.DiySqlMapper;
import com.ys.service.base.TbUserPayApplyService;
import com.ys.service.flow.YspTaskService;
import com.ys.util.CheckUtil;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.Flow;
import com.ys.vo.R;

@Service("tbUserPayApplyService")
public class TbUserPayApplyServiceImpl implements TbUserPayApplyService {

    @Autowired
    private TbUserPayApplyMapper tbUserPayApplyMapper;

    @Autowired
    private TbUserPayApplyItemMapper tbUserPayApplyItemMapper;

    @Autowired
    private CheckUtil checkUtil;

    @Autowired
    private YspTaskService yspTaskService;

    @Autowired
    private DiySqlMapper diySqlMapper;

    @Autowired
    private TbUserPaySlipMapper tbUserPaySlipMapper;


    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public R add(Map<String, Object> params,String subData) throws Exception {
        //验证
        String msg = yzMsg(params);
        if (msg != null){
            return R.error(msg);
        }
        TbUserPayApply tbUserPayApply = JSON.parseObject(JSON.toJSONString(params), TbUserPayApply.class);
        if (new TbUserPayApply().setBid(tbUserPayApply.getBid()).count() != 0){
            return R.error("该单号已存在，核实后再操作!");
        }
        // 验证是否有详情数据
        JSONArray detailJA = JSON.parseArray(subData);
        if (detailJA == null || detailJA.size() == 0) {
            return R.error("详情数据不能为空");
        }
        BigDecimal wagesPay = BigDecimal.ZERO;
        BigDecimal healthCompany = BigDecimal.ZERO;
        BigDecimal societyCompany = BigDecimal.ZERO;
        BigDecimal needPaySlip = BigDecimal.ZERO;
        BigDecimal personalTax = BigDecimal.ZERO;
        BigDecimal realityPaySlip = BigDecimal.ZERO;
        BigDecimal accumulationFundCompany = BigDecimal.ZERO;
        BigDecimal retirementCompany = BigDecimal.ZERO;
        BigDecimal loseWorkCompany = BigDecimal.ZERO;
        BigDecimal injuryCompany = BigDecimal.ZERO;
        BigDecimal birthCompany = BigDecimal.ZERO;
        List<TbUserPayApplyItem> tbUserPayApplyItemList = new ArrayList<>();
        String loginUserId = ShiroUtils.getUserId();
        TsUserChange tsUserChange = new TsUserChange().setUserId(loginUserId).setStatus(1).queryFirst();
        // 计算总金额
        for (int i = 0; i < detailJA.size(); i++) {
            JSONObject detailJO = detailJA.getJSONObject(i);
            TbUserPayApplyItem tbUserPayApplyItem = JSON.parseObject(JSON.toJSONString(detailJO), TbUserPayApplyItem.class);
            //判断是否重复
            if (new TbUserPayApplyItem().setUserPaySlipidid(tbUserPayApplyItem.getUserPaySlipidid()).count() != 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return R.error("工资条数据重复调用,核实后再操作!");
            }
            tbUserPayApplyItem.setUserPaySlipidid(Long.valueOf(detailJO.get("id").toString()));
            tbUserPayApplyItemList.add(tbUserPayApplyItem);
            wagesPay = wagesPay.add(new BigDecimal(detailJO.get("wagesPay").toString()));
            birthCompany = birthCompany.add(new BigDecimal(detailJO.get("birthCompany").toString()));//生育保险
            healthCompany = healthCompany.add(new BigDecimal(detailJO.get("healthCompany").toString()));
            needPaySlip = needPaySlip.add(new BigDecimal(detailJO.get("needPaySlip").toString()));
            personalTax = personalTax.add(new BigDecimal(detailJO.get("personalTax").toString()));
            realityPaySlip = realityPaySlip.add(new BigDecimal(detailJO.get("realityPaySlip").toString()));
            accumulationFundCompany = accumulationFundCompany.add(new BigDecimal(detailJO.get("accumulationFundCompany").toString()));
            retirementCompany = retirementCompany.add(new BigDecimal(detailJO.get("retirementCompany").toString()));
            loseWorkCompany = loseWorkCompany.add(new BigDecimal(detailJO.get("loseWorkCompany").toString()));
            injuryCompany = injuryCompany.add(new BigDecimal(detailJO.get("injuryCompany").toString()));
        }
        societyCompany = retirementCompany.add(loseWorkCompany).add(injuryCompany);
        tbUserPayApply.setWagesPay(wagesPay);
        tbUserPayApply.setHealthCompany(healthCompany.add(birthCompany));
        tbUserPayApply.setNeedPaySlip(needPaySlip);
        tbUserPayApply.setPersonalTax(personalTax);
        tbUserPayApply.setRealityPaySlip(realityPaySlip);
        tbUserPayApply.setAccumulationFundCompany(accumulationFundCompany);
        tbUserPayApply.setSocietyCompany(societyCompany);
        tbUserPayApply.setStatus(0);
        tbUserPayApply.setCreateChangeId(String.valueOf(tsUserChange.getId()));
        tbUserPayApply.setEffectStock(FlowEnum.EffectStock.NOT_EFFECT.value);
        tbUserPayApplyMapper.insert(tbUserPayApply);

        for (TbUserPayApplyItem tbUserPayApplyItem : tbUserPayApplyItemList) {
            tbUserPayApplyItem.setPid(tbUserPayApply.getId());
            tbUserPayApplyItemMapper.insert(tbUserPayApplyItem);
        }

        YsMenu ysMenu = new YsMenu().setId(MenuId.TB_UserPayApply).queryFirst();
        if (ysMenu.getFlowModelId().longValue() != 0L) {
            YspModel yspModel = new YspModel().setId(ysMenu.getFlowModelId()).queryFirst();
            String codeModel = yspModel.getCode();
            String createUserId = ShiroUtils.getUserId();
            yspTaskService.init(codeModel, tbUserPayApply.getBid(), createUserId);
        }
        return R.ok(AddDto.gotoEdit("/web/base/tbUserPayApply/toEdit?bid=" + tbUserPayApply.getBid()));
    }

    /**
     * 修改员工工资申请
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public R edit(Map<String, Object> params,String subData) throws Exception {

        //验证
        String msg = yzMsg(params);
        if (msg != null){
            return R.error(msg);
        }
        TbUserPayApply tbUserPayApply = JSON.parseObject(JSON.toJSONString(params), TbUserPayApply.class);
        if (new TbUserPayApply().setBid(tbUserPayApply.getBid()).where("A.id != " + tbUserPayApply.getId()).count() != 0){
            return R.error("该单号已存在，核实后再操作!");
        }

        // 验证是否有详情数据
        JSONArray detailJA = JSON.parseArray(subData);
        if (detailJA == null || detailJA.size() == 0) {
            return R.error("详情数据不能为空");
        }

        //判断是否实在制单中
        Integer flowState = yspTaskService.flowState(tbUserPayApply.getBid());

        if (flowState != 0){
            return R.error("员工工资申请单不是制单中,不允许保存!");
        }

        BigDecimal wagesPay = BigDecimal.ZERO;
        BigDecimal healthCompany = BigDecimal.ZERO;
        BigDecimal societyCompany = BigDecimal.ZERO;
        BigDecimal needPaySlip = BigDecimal.ZERO;
        BigDecimal personalTax = BigDecimal.ZERO;
        BigDecimal realityPaySlip = BigDecimal.ZERO;
        BigDecimal accumulationFundCompany = BigDecimal.ZERO;
        BigDecimal retirementCompany = BigDecimal.ZERO;
        BigDecimal loseWorkCompany = BigDecimal.ZERO;
        BigDecimal injuryCompany = BigDecimal.ZERO;
        BigDecimal birthCompany = BigDecimal.ZERO;
        List<TbUserPayApplyItem> tbUserPayApplyItemList = new ArrayList<>();
        // 计算总金额
        for (int i = 0; i < detailJA.size(); i++) {
            JSONObject detailJO = detailJA.getJSONObject(i);
            TbUserPayApplyItem tbUserPayApplyItem = JSON.parseObject(JSON.toJSONString(detailJO), TbUserPayApplyItem.class);
            tbUserPayApplyItem.setUserPaySlipidid(Long.valueOf(detailJO.get("userPaySlipidid").toString()));
            //判断是否重复
            if (new TbUserPayApplyItem().setUserPaySlipidid(tbUserPayApplyItem.getUserPaySlipidid()).where("A.id != " + tbUserPayApplyItem.getId()).count() != 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return R.error("工资条数据重复调用,核实后再操作!");
            }
            tbUserPayApplyItemList.add(tbUserPayApplyItem);
            wagesPay = wagesPay.add(new BigDecimal(detailJO.get("wagesPay").toString()));
            birthCompany = birthCompany.add(new BigDecimal(detailJO.get("birthCompany").toString()));//生育保险
            healthCompany = healthCompany.add(new BigDecimal(detailJO.get("healthCompany").toString()));
            needPaySlip = needPaySlip.add(new BigDecimal(detailJO.get("needPaySlip").toString()));
            personalTax = personalTax.add(new BigDecimal(detailJO.get("personalTax").toString()));
            realityPaySlip = realityPaySlip.add(new BigDecimal(detailJO.get("realityPaySlip").toString()));
            accumulationFundCompany = accumulationFundCompany.add(new BigDecimal(detailJO.get("accumulationFundCompany").toString()));
            retirementCompany = retirementCompany.add(new BigDecimal(detailJO.get("retirementCompany").toString()));
            loseWorkCompany = loseWorkCompany.add(new BigDecimal(detailJO.get("loseWorkCompany").toString()));
            injuryCompany = injuryCompany.add(new BigDecimal(detailJO.get("injuryCompany").toString()));
        }
        societyCompany = retirementCompany.add(loseWorkCompany).add(injuryCompany);
        tbUserPayApply.setWagesPay(wagesPay);
        tbUserPayApply.setHealthCompany(healthCompany.add(birthCompany));
        tbUserPayApply.setNeedPaySlip(needPaySlip);
        tbUserPayApply.setPersonalTax(personalTax);
        tbUserPayApply.setRealityPaySlip(realityPaySlip);
        tbUserPayApply.setAccumulationFundCompany(accumulationFundCompany);
        tbUserPayApply.setSocietyCompany(societyCompany);

        //批量删除
        diySqlMapper.deleteTbUserPayApplyItemPid(tbUserPayApply.getId());

        for (TbUserPayApplyItem tbUserPayApplyItem : tbUserPayApplyItemList) {
            tbUserPayApplyItem.setPid(tbUserPayApply.getId());
            tbUserPayApplyItemMapper.insert(tbUserPayApplyItem);
        }

        tbUserPayApplyMapper.update(tbUserPayApply);
        return R.ok(AddDto.gotoEdit("/web/base/tbUserPayApply/toEdit?bid=" + tbUserPayApply.getBid()));

    }

    /**
     * 删除
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public R delete(Long id) {
        try {
            TbUserPayApply tbUserPayApply = new TbUserPayApply().setId(id).queryFirst();
            if (tbUserPayApply == null){
                return R.error("员工工资申请单不存在!");
            }

            //判断是否实在制单中
            Integer flowState = yspTaskService.flowState(tbUserPayApply.getBid());

            if (flowState != 0){
                return R.error("员工工资申请单不是制单中,不允许保存!");
            }
            tbUserPayApplyMapper.deleteTrue(id);

            //批量删除
            diySqlMapper.deleteTbUserPayApplyItemPid(tbUserPayApply.getId());
            //删除任务
            yspTaskService.flowDelete(tbUserPayApply.getBid(),tbUserPayApply.getCreateChangeId());
        } catch (Exception e) {
            return R.error("删除失败!");
        }
        return R.ok("删除成功!");
    }


    /**
     * 流转
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public R flowPass(Long id, String loginUserId) throws Exception {
        TbUserPayApply entity = new TbUserPayApply().setId(id).queryFirst();
        String bid = entity.getBid();
        try {
            Flow f = yspTaskService.flowPass(bid, loginUserId);
            if (f.getCode() == Flow.Code.FAILED.value) {
                return R.error(f.getMsg());
            } else {

                if (f.getState() == Flow.State.FINISH.value) {
                    R r = flowFinish(entity);
                    if (r.getCode() == 1) {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return R.error(r.getMsg());
                    }
                }
                return R.ok();
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return R.error("服务器正在开小差，请联系客服");
        }
    }


    /**
     * 退回
     * @param id
     * @param reason
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public R flowRefuse(Long id, String reason, String loginUserId) throws Exception {
        try {
            TbUserPayApply entity = new TbUserPayApply().setId(id).queryFirst();
            String bid = entity.getBid();
            R r = yspTaskService.flowRefuse(bid, reason, loginUserId);
            if (r.getCode() == 1){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return R.error(r.getMsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return R.error("服务器正在开小差，请联系客服");
        }
        return R.ok();
    }

    /**
     * 驳回
     * @param id
     * @param reason
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public R flowInit(Long id, String reason, String loginUserId) throws Exception {
        try {
            TbUserPayApply entity = new TbUserPayApply().setId(id).queryFirst();
            String bid = entity.getBid();
            R r = yspTaskService.flowInit(bid, reason, loginUserId);
            if (r.getCode() == 1){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return R.error(r.getMsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return R.error("服务器正在开小差，请联系客服");
        }
        return R.ok();
    }

    private String yzMsg(Map<String, Object> infoMap){
        //验证导入字段
        String backMsg = "";
        //获取pc字段数据
        List<YsCol> listField = new YsCol()
                .where("A.MenuId = "+ MenuId.TB_UserPayApply)
                .setOrderby("A.SortNo ASC")
                .queryList();
        //验证必填字段的非空；其他字段的长度限制、类型
        try {
            backMsg = checkUtil.checkTableColLengthAndDataType(
                    "TB_UserPayApply",
                    listField,
                    infoMap,
                    ysColItem->ysColItem.getFormEditRequired()!=null && ysColItem.getFormEditRequired() == 1
            );
            if(backMsg!=null && !"".equals(backMsg)){//若有误，则跳过这条信息
                return backMsg;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public R flowFinish(TbUserPayApply entity) throws Exception {
        try {
        entity.setEffectStock(FlowEnum.EffectStock.EFFECTED.value);
        tbUserPayApplyMapper.update(entity);

        //查询子件信息
        List<TbUserPayApplyItem> sonlist = new TbUserPayApplyItem()
                .setPid(entity.getId())
                .queryList();

        for (TbUserPayApplyItem tbUserPayApplyItem : sonlist){
            //将工资条状态修改为待发放
            TbUserPaySlip tbUserPaySlip = new TbUserPaySlip().setId(Long.valueOf(tbUserPayApplyItem.getUserPaySlipidid().toString()));
            tbUserPaySlip.setSlipStatus(2);//工资状态（0空、1个税已导入、2待发放、3已发放）
            tbUserPaySlipMapper.update(tbUserPaySlip);

        }

        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }

        return R.ok();
    }


    /**
     * 新增流程
     * @param menuId 菜单id
     * @param bid 单号
     */
    private void addYsMenu(Long menuId,String bid){
        try {
            YsMenu ysMenu = new YsMenu().setId(menuId).queryFirst();
            if (ysMenu.getFlowModelId().longValue() != 0L) {
                YspModel yspModel = new YspModel().setId(ysMenu.getFlowModelId()).queryFirst();
                String codeModel = yspModel.getCode();
                String createUserId = ShiroUtils.getUserId();
                yspTaskService.init(codeModel, bid, createUserId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public void updateTotalCount() throws Exception {
        //List<TbUserPayApply> tbUserPayApplyList = new TbUserPayApply().setPointMonth("2025-02").queryList();
        List<TbUserPayApply> tbUserPayApplyList = new TbUserPayApply().queryList();
        for(TbUserPayApply tbUserPayApply:tbUserPayApplyList){
            BigDecimal wagesPay = BigDecimal.ZERO;
            BigDecimal healthCompany = BigDecimal.ZERO;
            BigDecimal societyCompany = BigDecimal.ZERO;
            BigDecimal needPaySlip = BigDecimal.ZERO;
            BigDecimal personalTax = BigDecimal.ZERO;
            BigDecimal realityPaySlip = BigDecimal.ZERO;
            BigDecimal accumulationFundCompany = BigDecimal.ZERO;
            BigDecimal retirementCompany = BigDecimal.ZERO;
            BigDecimal loseWorkCompany = BigDecimal.ZERO;
            BigDecimal injuryCompany = BigDecimal.ZERO;
            BigDecimal birthCompany = BigDecimal.ZERO;
            List<TbUserPayApplyItem> tbUserPayApplyItemList = new TbUserPayApplyItem().setPid(tbUserPayApply.getId()).queryList();
            // 计算总金额
            for (TbUserPayApplyItem item:tbUserPayApplyItemList) {

                TbUserPaySlip tbUserPaySlip = new TbUserPaySlip().setId(item.getUserPaySlipidid()).queryFirst();

                wagesPay = wagesPay.add(tbUserPaySlip.getWagesPay());//应发工资
                birthCompany = birthCompany.add(tbUserPaySlip.getBirthCompany());//生育保险
                healthCompany = healthCompany.add(tbUserPaySlip.getHealthCompany());//医疗保险（单位）
                needPaySlip = needPaySlip.add(tbUserPaySlip.getNeedPaySlip());//应付工资
                personalTax = personalTax.add(tbUserPaySlip.getPersonalTax());//个人所得税
                realityPaySlip = realityPaySlip.add(tbUserPaySlip.getRealityPaySlip());//实发工资
                accumulationFundCompany = accumulationFundCompany.add(tbUserPaySlip.getAccumulationFundCompany());//公积金（单位）
                retirementCompany = retirementCompany.add(tbUserPaySlip.getRetirementCompany());//养老保险（单位）
                loseWorkCompany = loseWorkCompany.add(tbUserPaySlip.getLoseWorkCompany());//失业保险（单位）
                injuryCompany = injuryCompany.add(tbUserPaySlip.getInjuryCompany());//工伤保险（单位）
            }
            societyCompany = retirementCompany.add(loseWorkCompany).add(injuryCompany);
            tbUserPayApply.setWagesPay(wagesPay);
            tbUserPayApply.setHealthCompany(healthCompany.add(birthCompany));
            tbUserPayApply.setNeedPaySlip(needPaySlip);
            tbUserPayApply.setPersonalTax(personalTax);
            tbUserPayApply.setRealityPaySlip(realityPaySlip);
            tbUserPayApply.setAccumulationFundCompany(accumulationFundCompany);
            tbUserPayApply.setSocietyCompany(societyCompany);
            tbUserPayApplyMapper.update(tbUserPayApply);
        }





    }



    //获取列表数据
    @Override
    @Transactional
    public Map<String,Object> getExtraParam( String bid) throws Exception{
        Map<String,Object> entity = new TbUserPayApply().setBid(bid)
                .addLeftJoin("YS_ProcInst Ysp ON Ysp.bid = A.bid")//流程表
                .addField("Ysp.updateTime AS updateTime1")//
                .addLeftJoin("TS_OrganizationInfo oi ON oi.Orgid = A.Orgid")//所属公司
                .addField("oi.OrgName AS orgName")//
                .queryFirstMap();
        return entity;
    }
    //获取子列表数据
    @Override
    @Transactional
    public List<Map<String, Object>> getItemList( String bid) throws Exception{
        TbUserPayApply entity = new TbUserPayApply().setBid(bid).queryFirst();
        List<Map<String, Object>> sonlist = new TbUserPayApplyItem()
                .setPid(entity.getId())
                .addLeftJoin("TB_UserPaySlip R ON R.id = A.UserPaySlipidid")//员工工资表
                .addField("R.pointMonth AS pointMonth")//发薪月份
                .addField("R.attendancePerformance AS attendancePerformance")//考勤绩效
                .addField("R.otherPerformance AS otherPerformance")//其他绩效
                .addField("R.otherPerformanceRemark AS otherPerformanceRemark")//其他绩效备注
                .addField("R.wagesPay AS wagesPay")//应发工资
                .addField("R.retirementCompany AS retirementCompany")//养老保险（单位）
                .addField("R.retirementPersonal AS retirementPersonal")//养老保险（个人）
                .addField("R.loseWorkCompany AS loseWorkCompany")//失业保险（单位）
                .addField("R.loseWorkPersonal AS loseWorkPersonal")//失业保险（个人）
                .addField("R.injuryCompany AS injuryCompany")//工伤保险（单位）
                .addField("R.healthCompany AS healthCompany")//医疗保险（单位）
                .addField("R.healthPersonal AS healthPersonal")//医疗保险（个人）
                .addField("R.birthCompany AS birthCompany")//生育保险（单位）
                .addField("R.accumulationFundCompany AS accumulationFundCompany")//公积金（单位）
                .addField("R.accumulationFundPersonal AS accumulationFundPersonal")//公积金（个人）
                .addField("R.otherCost AS otherCost")//其他费用
                .addField("R.otherCostRemark AS otherCostRemark")//其他费用备注
                .addField("R.needPaySlip AS needPaySlip")//应付工资
                .addField("R.personalTax AS personalTax")//个人所得税
                .addField("R.realityPaySlip AS realityPaySlip")//实发工资
                .addField("R.performanceSalary AS perbasedSalary")//绩效薪资
                .addField("R.quarterBonus AS quarterlyBonus")//季度提奖
                .addField("R.yearEndBonus AS annualBonus")//年终奖

                .addLeftJoin("TS_UserInfo U ON U.UserId = R.UserId")//用户信息表
                .addField("(U.UserId + '/' + U.UserName) AS _ys_nameLinked")//工号/姓名
                .addLeftJoin("TS_OrganizationInfo oi ON oi.OrgId = U.DeptId")//组织机构表
                .addField("oi.DeptName AS deptName")//部门

                .addLeftJoin("THR_UserArchives UAS ON UAS.UserId = R.UserId")//用户档案表
                .addField("UAS.UserState AS userState")//员工状态

                .addLeftJoin("TS_OrganizationInfo O ON O.OrgId = R.OrgId")//组织机构表
                .addField("O.OrgName AS orgName")//所属公司

                .addLeftJoin("TB_UserMonthPerformance MP ON MP.Id = R.MonthPerformanceId")//员工月度绩效信息表
                .addField("MP.Rank AS rank")//岗位职级
                .addField("MP.Type AS type")//岗位工种(1总经理 2副总经理 3责任月薪型员工 4绩效提奖型员工 5研发人员)
                .addField("MP.JobBonus AS jobBonus")//岗位提奖/月

                .addLeftJoin("TB_JobLevel JB ON JB.Id = R.RoleLevelId")//岗位职级管理表
                .addField("JB.Category AS category")//岗位类别
                .addField("JB.Categories AS categories")//职级大类(0A 1B 2C 3D 4P)
                .addField("JB.FixsalaryStandard AS fixsalaryStandard")//定薪标准
                .addField("JB.SalaryStandard AS salaryStandard")//发薪标准
                .addField("JB.BasicSalary AS basicSalary")//基本底薪
                .addField("JB.PositionSalary AS positionSalary")//岗位薪资
                .addField("JB.TransSubsidy AS transSubsidy")//交通补贴
                .addField("JB.CateringSubsidy AS cateringSubsidy")//餐饮补贴
                //.addField("JB.JobPromotion AS jobPromotion")//岗位提奖/月
                .addField("O.orgName+R.pointMonth AS orgNamePointMonth")//判断重复使用
                .queryMap();
        return sonlist;
    }




}
